<template>
    <div>

        <el-form :inline="true" :model="searchForm">
            <el-form-item>
                <el-button type="primary" @click="dialogFormVisible = true">新增</el-button>
            </el-form-item>
        </el-form>

        <el-table
                :data="tableData"
                style="width: 100%; margin-bottom: 20px"
                row-key="id"
                border
                stripe
                default-expand-all
        >
            <el-table-column prop="name" label="名称"/>
            <el-table-column prop="permCode" label="权限编码"/>
            <el-table-column width="100" prop="icon" label="图标"/>
            <el-table-column width="75"  prop="type" label="类型">
                <template v-slot:default="scope">
                    <el-tag type="success" v-if="scope.row.type === 0 ">目录</el-tag>
                    <el-tag type="warning" v-if="scope.row.type === 1 ">菜单</el-tag>
                    <el-tag type="info" v-if="scope.row.type === 2 ">按钮</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="path" label="菜单URL"/>
            <el-table-column prop="component" label="组件"/>
<!--            <el-table-column prop="order" label="排序"/>-->
            <el-table-column width="75" prop="state" label="状态">
                <template v-slot:default="scope">
                    <el-button size="small" type="success" v-if="scope.row.state === 1 ">正常</el-button>
                    <el-button size="small" type="warning" v-else>禁用</el-button>
                </template>
            </el-table-column>
            <el-table-column prop="operation" label="操作">
                <template v-slot:default="scope">
                    <el-popconfirm title="你确定要删除吗?">
                        <template #reference>
                            <el-button type="danger" @click="handleDelete(scope.row.id)">
                                <el-icon>
                                    <Delete/>
                                </el-icon>
                            </el-button>
                        </template>
                    </el-popconfirm>
                    <el-button type="primary" @click="handleEdit(scope.row.id)">
                        <el-icon>
                            <Edit/>
                        </el-icon>
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-dialog title="菜单信息" :model-value="dialogFormVisible" width="600px" @closed="resetForm('editForm')">
            <el-form :model="editForm" :rules="editFormRules" ref="editForm">
                <el-form-item label="上级菜单" prop="parentId" label-width="100px">
                    <!--模拟树形下拉框-->
                    <el-select v-model="editForm.parentId" placeholder="请选择上级菜单">
                        <template v-for="item in tableData">
                            <el-option :label="item.name" :value="item.id"></el-option>
                            <template v-for="child in item.children">
                                <el-option :label="child.name" :value="child.id">
                                    <span>{{ '- ' + child.name }}</span>
                                </el-option>
                            </template>
                        </template>
                    </el-select>
                </el-form-item>
                <el-form-item label="菜单名称" prop="name" label-width="100px">
                    <el-input v-model="editForm.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="权限编码" prop="permCode" label-width="100px">
                    <el-input v-model="editForm.permCode" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="图标" prop="icon" label-width="100px">
                    <el-input v-model="editForm.icon" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="菜单URL" prop="path" label-width="100px">
                    <el-input v-model="editForm.path" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="菜单组件" prop="component" label-width="100px">
                    <el-input v-model="editForm.component" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="类型" prop="type" label-width="100px">
                    <el-radio-group v-model="editForm.type">
                        <el-radio :label="0">目录</el-radio>
                        <el-radio :label="1">菜单</el-radio>
                        <el-radio :label="2">按钮</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="状态" prop="state" label-width="100px">
                    <el-radio-group v-model="editForm.state">
                        <el-radio :label="0">禁用</el-radio>
                        <el-radio :label="1">正常</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="排序号" prop="order" label-width="100px">
                    <el-input-number v-model="editForm.order" :min="1" label="排序号">1</el-input-number>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="resetForm('editForm')">取 消</el-button>
                <el-button type="primary" @click="submitEditForm('editForm')">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {Delete, Edit} from "@element-plus/icons";

export default {
    name: "Menu",
    components: {Delete, Edit},
    data() {
        return {
            searchForm: {
                name: ''
            },
            tableData: [],
            multipleSelection: [],
            dialogFormVisible: false,
            editForm: {},
            editFormRules: {
                parentId: [
                    {required: true, message: '请选择上级菜单', trigger: 'blur'}
                ],
                name: [
                    {required: true, message: '请输入名称', trigger: 'blur'}
                ],
                permCode: [
                    {required: true, message: '请输入权限编码', trigger: 'blur'}
                ],
                type: [
                    {required: true, message: '请选择状态', trigger: 'blur'}
                ],
                order: [
                    {required: true, message: '请填入排序号', trigger: 'blur'}
                ],
                state: [
                    {required: true, message: '请选择状态', trigger: 'blur'}
                ]
            }
        }
    },
    methods: {
        getMenuTree() {
            this.axios.get(`/system/menus/list`).then(resp => {
                this.tableData = resp.data.data;
                console.log(resp.data.data)
            }).catch(err => {

            })
        },
        submitEditForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    console.log(this.editForm)
                    this.$http.post('/system/menus/' + (this.editForm.id ? "update" : "save"), this.editForm)
                        .then(res => {
                            console.log(res.data);
                            this.resetForm(formName);
                        })
                } else {
                    console.log('error submit');
                    return false;
                }
            });
        },
        handleEdit(id) {
            this.axios.get("/system/menus/info/" + id.toLocaleString()).then(resp => {
                this.editForm = resp.data.data;
                console.log(resp.data.data)
                this.dialogFormVisible = true;
            })
        },
        handleDelete(id) {
            this.axios.post("/system/menu/delete", {
                id: id
            }).then(res => {
                console.log(res)
            })
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.editForm = {}
            this.dialogFormVisible = false
        },
    },
    created() {
        this.getMenuTree()
    }
}
</script>

<style scoped>

</style>
